package com.nodecollege.cloud.controller.admin;

import com.nodecollege.cloud.common.annotation.AdminLoginAnnotation;
import com.nodecollege.cloud.common.annotation.ApiAnnotation;
import com.nodecollege.cloud.common.model.NCResult;
import com.nodecollege.cloud.common.model.po.UpmsApi;
import com.nodecollege.cloud.common.model.po.UpmsApiVisit;
import com.nodecollege.cloud.common.model.vo.ApiVisitQuery;
import com.nodecollege.cloud.common.model.vo.ApiVisitVO;
import com.nodecollege.cloud.service.ApiService;
import com.nodecollege.cloud.service.ApiVisitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author LC
 * @date 2019/12/21 20:26
 */
@RestController
@AdminLoginAnnotation
@RequestMapping("/api")
public class ApiController {

    @Autowired
    private ApiService apiService;

    @Autowired
    private ApiVisitService apiVisitService;

    /**
     * 获取接口树
     *
     * @return
     */
    @ApiAnnotation(modularName = "接口管理", description = "获取接口树")
    @PostMapping("/getApiTree")
    public NCResult<UpmsApi> getApiTree() {
        return NCResult.ok(apiService.getApiTree());
    }

    /**
     * 清除接口缓存
     */
    @ApiAnnotation(modularName = "接口管理", description = "清除接口缓存", accessSource = 2)
    @RequestMapping("/clearApiListCache")
    public NCResult clearApiListCache() {
        apiService.clearApiListCache();
        return NCResult.ok("清除api缓存成功！");
    }

    @ApiAnnotation(modularName = "接口管理", description = "添加接口", accessSource = 2)
    @PostMapping("/initApiList")
    public NCResult<UpmsApi> initApiList(@RequestBody List<UpmsApi> annList) {
        return NCResult.ok(apiService.initApiList(annList));
    }

    @ApiAnnotation(modularName = "接口访问管理", description = "添加接口访问数据", accessSource = 2)
    @PostMapping("/addApiVisitList")
    public NCResult addApiVisitList(@RequestBody List<UpmsApiVisit> apiVisitList) {
        apiVisitService.addApiVisitList(apiVisitList);
        return NCResult.ok("添加接口访问数据成功！");
    }

    @ApiAnnotation(modularName = "接口访问管理", description = "查询接口访问情况")
    @RequestMapping("/getApiVisitList")
    public NCResult<ApiVisitVO> getApiVisitList(@RequestBody ApiVisitQuery apiVisitQuery) {
        return apiVisitService.getApiVisitList(apiVisitQuery);
    }
}
